package com.qzdsoft.erpcloud.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.domain.sys.SysTableAuthField;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysTableAuthFieldService;
import com.qzdsoft.erpcloud.service.sys.SysTableAuthService;
import com.qzdsoft.erpcloud.vo.sys.SysTableAuthFieldQueryInfo;
import com.qzdsoft.erpcloud.vo.sys.SysTableAuthFieldQueryParam;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.utils.enums.CommonSfEnum;
import com.qzdsoft.vo.DataTablePo;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.sys.SysTableAuthFieldTypeEnum;

/**
 * Created by Admin
 * 表格权限配置
 */
@Controller
@RequestMapping("/sys/table/auth/field")
public class SysTableAuthFieldController {
	
	private static final Logger logger = LoggerFactory.getLogger(SysTableAuthFieldController.class);
	private static final String BASEPATH = "sys/table";
	
    @Autowired
    private SysTableAuthFieldService sysTableAuthFieldService;
    
    @Autowired
    private SysTableAuthService sysTableAuthService;

    /**
     *
     * @param request
     * @param model
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {
        List<EnumPo> sfList = new ArrayList<EnumPo>();
        Map<Object, Object> sfMap = new HashMap<Object, Object>();
        for(CommonSfEnum e : CommonSfEnum.values()){
            SysUtils.enums(sfList, sfMap, e.getKey(), e.getVal());
        }

        List<EnumPo> fieldTypeList = new ArrayList<EnumPo>();
        Map<Object, Object> fieldTypeMap = new HashMap<Object, Object>();
        for(SysTableAuthFieldTypeEnum e : SysTableAuthFieldTypeEnum.values()){
            SysUtils.enums(fieldTypeList, fieldTypeMap, e.getKey(), e.getVal());
        }

        model.addAttribute("sfList", sfList);
        model.addAttribute("sfMap", sfMap);

        model.addAttribute("fieldTypeList", fieldTypeList);
        model.addAttribute("fieldTypeMap", fieldTypeMap);

        return "sys/table_auth_field";
    }

    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse data(HttpServletRequest request, SysTableAuthFieldQueryInfo info) {
//        Integer count = 0;
//        List list = null;
//        Integer tableId = param.getTable_id();
//        if(null != tableId){
//            count = sysTableAuthFieldService.selectDataCount(tableId);
//            list = sysTableAuthFieldService.selectData(tableId, param.getiDisplayStart(), param.getiDisplayLength());
//        }
//        return new DataTablePo(count, list, param.getsEcho());
    	return sysTableAuthFieldService.selectData(info);
    }
    
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public ModelAndView addinit(Integer tableId) {
    	ModelAndView mv =new ModelAndView();
    	mv.addObject("tableId", tableId);
    	mv.setViewName("sys/tableinit/add");
    	return mv;
    }
    
    @Permission(validate=false)
    @SysLog(description="表格权限配置新增保存")
    @ResponseBody
    @RequestMapping("/add")
    public Response add(SysTableAuthField info) {
    	
    	return sysTableAuthFieldService.saveOrUpd(info);
    }
    
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
    	ModelAndView mv =new ModelAndView();
    	mv.addObject("obj", sysTableAuthFieldService.selectById(id));
    	mv.setViewName("sys/tableinit/edit");
    	return mv;
    }

    /**
     *
     * @param request
     * @param sysTableAuthField
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="表格权限配置修改保存")
    @ResponseBody
    @RequestMapping("/edit_execute")
    public Response editExecute(HttpServletRequest request, SysTableAuthField sysTableAuthField) {
        return sysTableAuthFieldService.saveOrUpd(sysTableAuthField);
    }

    /**
     *
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="表格权限配置删除")
    @ResponseBody
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return sysTableAuthFieldService.delete(id);
    }
    
    
    /**
     * 表格配置
     * tableinit
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/tableinit")
    public ModelAndView tableinit(Integer id) {
    	ModelAndView mv = new ModelAndView();
    	mv.addObject("obj", sysTableAuthService.selectById(id));
//    	mv.setViewName(BASEPATH+"/tableItem");
    	mv.setViewName("sys/tableinit/index");
    	return mv;
    }
    /**
     * 表格字段查询
     * @param info
     * @return
     */
//    @Permission(validate=false)
//    @ResponseBody
//    @RequestMapping("/table-fields")
//    public ModelAndView fields(SysTableAuthFieldQueryInfo info) {
//    	ModelAndView mv = new ModelAndView();
//      List<EnumPo> fieldTypeList = new ArrayList<EnumPo>();
//      Map<Object, Object> fieldTypeMap = new HashMap<Object, Object>();
//      for(SysTableAuthFieldTypeEnum e : SysTableAuthFieldTypeEnum.values()){
//          SysUtils.enums(fieldTypeList, fieldTypeMap, e.getKey(), e.getVal());
//      }
//      	mv.addObject("fieldTypeList", fieldTypeList);
//      	mv.addObject("tableId", info.getTableId());
//    	mv.addObject("items", sysTableAuthFieldService.selectByTableId(info));
////    	mv.addObject("obj", sysTableAuthService.selectById(id));
//    	mv.setViewName(BASEPATH+"/fieldItem");
//    	return mv;
//    	
//    }

}
